/*************************************************************************
	> File Name: thread_pool.c
	> Author: Maureen 
	> Mail: 1437867946@qq.com 
	> Created Time: Tue 24 Nov 2020 06:44:24 PM CST
 ************************************************************************/

#include "thread_pool.h"

char buff[105][1024] = {0};//接收客户端发送过来的字符串
int ind = 0;

void task_queue_init(struct task_queue *taskQueue, int size) {
    taskQueue->size = size;
    taskQueue->total = 0;
    taskQueue->head = 0;
    taskQueue->tail = 0;
    taskQueue->data = calloc(size, sizeof(int));
    pthread_mutex_init(&taskQueue->mutex, NULL);
    pthread_cond_init(&taskQueue->cond, NULL);
    return ;
}

void task_queue_push(struct task_queue *taskQueue, int fd) {
    //队列的操作要安全,进行加锁
    pthread_mutex_lock(&taskQueue->mutex);
    if (taskQueue->total == taskQueue->size) { //队列满了,再有新任务就忽略
        pthread_mutex_unlock(&taskQueue->mutex);
        DBG(YELLOW"<Debug> :"NONE" task queue is full!\n");
        return ;
    }
    taskQueue->data[taskQueue->tail] = fd;
    taskQueue->total++;
    DBG(GREEN"<Push> : "NONE":%d\n", fd);
    if (++taskQueue->tail == taskQueue->size) {
        DBG(YELLOW"<Debug> : "NONE" task queue push reach end!\n");
        taskQueue->tail = 0;
    }
    pthread_cond_signal(&taskQueue->cond);//发信号通知有任务来了
    pthread_mutex_unlock(&taskQueue->mutex);
}

int task_queue_pop(struct task_queue *taskQueue) {
    pthread_mutex_lock(&taskQueue->mutex);
    while (taskQueue->total == 0) { //没任务就等待
        DBG(YELLOW"<Debug> : "NONE" task queue is empty!\n");
        pthread_cond_wait(&taskQueue->cond, &taskQueue->mutex);
    }
    int fd = taskQueue->data[taskQueue->head];
    taskQueue->total--;
    if (++taskQueue->head == taskQueue->size) {
        DBG(YELLOW"<Debug> : "NONE" task queue pop reach end!\n");
        taskQueue->head = 0;
    }
        int res = recv(fd, (void *)buff[ind], sizeof(buff[ind]), 0);
        if (res == 0) { //客户端已经关闭连接
            printf("Connection Closed!\n");
            close(fd);
        } else if (res > 0) {//接收到数据就回发给客户端
            printf(GREEN"<%ld> ：" NONE"%s\n", pthread_self(), buff[ind]);
            send(fd, (void *)buff[ind], strlen(buff[ind]), 0);
            ind++;
        } else if (res == -1) {
            DBG(RED"<Error> : "NONE" error occurs when recv data from client!\n");
            close(fd);
        }
    pthread_mutex_unlock(&taskQueue->mutex);
    return fd;
}

void *thread_run(void *arg) {
    pthread_detach(pthread_self());
    struct task_queue *taskQueue = (struct task_queue *)arg;
    while (1) {
        //usleep(100000);
        int fd = task_queue_pop(taskQueue);
        //pthread_mutex_lock(&taskQueue->mutex);
        //接收客户端发送的字符串
       /* int res = recv(fd, (void *)buff[ind], sizeof(buff[ind]), 0);
        if (res == 0) { //客户端已经关闭连接
            close(fd);
        } else if (res > 0) {//接收到数据就回发给客户端
            printf(GREEN"<%ld> ：" NONE"%s\n", pthread_self(), buff[ind]);
            send(fd, (void *)buff[ind], sizeof(buff[ind]), 0);
            ind++;
        } else if (res == -1) {
            DBG(RED"<Error> : "NONE" error occurs when recv data from client!\n");
            close(fd);
        }*/
        //pthread_mutex_unlock(&taskQueue->mutex);
    }
}
